class_name LevelRegion
extends Marker2D

@export var region_data: RegionData
@onready var unit_born_point_parent: Node2D = $UnitBornPoints
@onready var crates_parent: Node2D = $Crates
@onready var player_born_random_points_parent: Node2D = $PlayerBornRandomPoints
@onready var ground_tilemap_layer: TileMapLayer = $GroundTilemapLayer
@onready var doors: Node2D = $Doors
@onready var broken_blocks: Node2D = $BrokenBlocks
@onready var scene_torch_parent: Node2D = $SceneTorch
@onready var switch_parent: Node2D = $switch
@onready var water_parent: Node2D = $Water
@onready var damage_areas_parent: Node2D = $DamageAreas
@onready var dark_spaces_parent: Node2D = $DarkSpaces
@onready var trap_parent: Node2D = $Trap

# exit ground
@onready var exit_tilemap_layer_top: TileMapLayer = $ExitTilemapLayerTop
@onready var exit_tilemap_layer_right: TileMapLayer = $ExitTilemapLayerRight
@onready var exit_tilemap_layer_bottom: TileMapLayer = $ExitTilemapLayerBottom
@onready var exit_tilemap_layer_left: TileMapLayer = $ExitTilemapLayerLeft


func init_exit_tilemap_layer():
	if region_data.position == Vector2(0, 0):
		exit_tilemap_layer_top.enabled = true
		exit_tilemap_layer_left.enabled = true
	elif region_data.position == Vector2(1600, 0):
		exit_tilemap_layer_top.enabled = true
	elif region_data.position == Vector2(3200, 0):
		exit_tilemap_layer_top.enabled = true
		exit_tilemap_layer_right.enabled = true
	elif region_data.position == Vector2(0, 1600):
		exit_tilemap_layer_left.enabled = true
	elif region_data.position == Vector2(3200, 1600):
		exit_tilemap_layer_right.enabled = true
	elif region_data.position == Vector2(0, 3200):
		exit_tilemap_layer_left.enabled = true	
		exit_tilemap_layer_bottom.enabled = true
	elif region_data.position == Vector2(1600, 3200):
		exit_tilemap_layer_bottom.enabled = true
	elif region_data.position == Vector2(3200, 3200):
		exit_tilemap_layer_bottom.enabled = true
		exit_tilemap_layer_right.enabled = true
			

func get_traps():
	var result: Array = []
	for index in range(trap_parent.get_child_count()):
		var trap = trap_parent.get_child(index)
		result.append(trap)
	return result


func get_waters() -> Array[Water]:
	var result: Array[Water] = []
	for index in range(water_parent.get_child_count()):
		var water = water_parent.get_child(index)
		result.append(water)
	return result
	

func get_damage_areas() -> Array[DotDamageArea]:
	var result: Array[DotDamageArea] = []
	for index in range(damage_areas_parent.get_child_count()):
		var damage_area = damage_areas_parent.get_child(index)
		result.append(damage_area)
	return result
	

func get_dark_spaces() -> Array[DarkSpace]:
	var result: Array[DarkSpace] = []
	for index in range(dark_spaces_parent.get_child_count()):
		var dark_space = dark_spaces_parent.get_child(index)
		result.append(dark_space)
	return result


func init_by_data(data: RegionData):
	region_data = data
	global_position = region_data.position
	

func get_torchs() -> Array[SceneTorch]:
	var result: Array[SceneTorch] = []
	for index in range(scene_torch_parent.get_child_count()):
		var torch = scene_torch_parent.get_child(index)
		result.append(torch)
	return result


func get_switches() -> Array[TorchSwitch]:
	var result: Array[TorchSwitch] = []
	for index in range(switch_parent.get_child_count()):
		var switch = switch_parent.get_child(index)
		result.append(switch)
	return result
	

func get_doors() -> Array[LockDoor]:
	var result: Array[LockDoor] = []
	for index in range(doors.get_child_count()):
		var door = doors.get_child(index)
		result.append(door)
	return result


func get_broken_blocks() -> Array[BrokenBlock]:
	var result: Array[BrokenBlock] = []
	for index in range(broken_blocks.get_child_count()):
		var block = broken_blocks.get_child(index)
		result.append(block)
	return result


func get_ground_tilemap_layer():
	return ground_tilemap_layer


func get_all_born_point_data() -> Array[UnitBornPointData]:
	var result: Array[UnitBornPointData] = []
	for index in range(unit_born_point_parent.get_child_count()):
		var unit_born_point = unit_born_point_parent.get_child(index) as UnitBornPoint
		result.append(unit_born_point.data) 
	
	return result
	

func get_all_born_point() -> Array[UnitBornPoint]:
	var result: Array[UnitBornPoint] = []
	for index in range(unit_born_point_parent.get_child_count()):
		var unit_born_point = unit_born_point_parent.get_child(index) as UnitBornPoint
		result.append(unit_born_point) 
	
	return result


func find_born_point_by_id(id: String):
	for index in range(unit_born_point_parent.get_child_count()):
		var unit_born_point = unit_born_point_parent.get_child(index) as UnitBornPoint
		var data = unit_born_point.data
		if data.id == id:
			return unit_born_point
	return null


func get_all_crates_data() -> Array[CrateData]:
	var result: Array[CrateData] = []
	
	for index in range(crates_parent.get_child_count()):
		var crate_child = crates_parent.get_child(index) as WoodCrate
		result.append(crate_child.crate_data)
		
	return result
	

func get_all_crates() -> Array[WoodCrate]:
	var result: Array[WoodCrate] = []
	
	for index in range(crates_parent.get_child_count()):
		var crate_child = crates_parent.get_child(index) as WoodCrate
		result.append(crate_child)
		
	return result

	
func find_crate_by_id(id: String) -> WoodCrate:
	for index in range(crates_parent.get_child_count()):
		var crate_child = crates_parent.get_child(index) as WoodCrate
		if crate_child.crate_data.id == id:
			return crate_child
	return null
	

func get_all_player_born_random_points():
	var result: Array[Marker2D] = []
	
	for index in range(player_born_random_points_parent.get_child_count()):
		var point = player_born_random_points_parent.get_child(index) as Marker2D
		result.append(point)
		
	return result
	
	
	
	
	
	
	
	
	
